package cn.lzj.ymcc.mq;

import cn.lzj.ymcc.constants.RocketMQConstants;
import cn.lzj.ymcc.domain.MediaFile;
import cn.lzj.ymcc.result.JSONResult;
import cn.lzj.ymcc.service.IMediaFileService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
@Slf4j
@Component
@RocketMQMessageListener(
        //topic名称
        topic = RocketMQConstants.ROKETMQ_TOPIC_MEDIA_PUSHSTREAM,
        //消费者组名
        selectorExpression = RocketMQConstants.ROKETMQ_TAGS_MEDIA_PUSHSTREAM,
        consumerGroup = "service-media-consumer-pushstream-video",
        messageModel = MessageModel.CLUSTERING)
public class RocketMQConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IMediaFileService mediaFileService;

    @Override
    public void onMessage(MessageExt messageExt) {
        log.info("推流正在执行！！！",messageExt);
        if(messageExt.getBody()==null||messageExt.getBody().length==0) {
            return;
        }
        String s = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        log.info("内容是",s);
        MediaFile mediaFile = JSON.parseObject(s, MediaFile.class);
        JSONResult jsonResult = mediaFileService.handleFile2m3u8(mediaFile);
        log.info("推流已经完成！！"+jsonResult);
    }
}
